//
//  HRRecordAudioRootView.m
//  Hear
//
//  Created by mac on 2017/3/31.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRRecordAudioRootView.h"
#import "HRSelectBackGroundMusicView.h"
#import "HRShowSoundContentView.h"
@interface HRRecordAudioRootView ()


@property (nonatomic, strong)HRSelectBackGroundMusicView *musicView;
@property (nonatomic, strong)UIButton *recordButton;
@property (nonatomic, strong)HRShowSoundContentView *soundContentView;
@property (nonatomic, strong)UIButton *againRecordButton;

@end
@implementation HRRecordAudioRootView

-(HRShowSoundContentView *)soundContentView{
    if (_soundContentView == nil){
        _soundContentView = [HRShowSoundContentView new];
        [self addSubview:self.soundContentView];
        WeakSelf
        [self.soundContentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf).offset(100);
            make.left.width.equalTo(weakSelf);
            make.height.mas_equalTo(100);
        }];
        
    }
    return _soundContentView;
}

-(HRSelectBackGroundMusicView *)musicView
{
    if (_musicView ==  nil){
        _musicView = [HRSelectBackGroundMusicView new];
        [self addSubview:self.musicView];
        WeakSelf
        _musicView.selectMusicBlock = ^(NSString *musicName){
            if (weakSelf.addMusicBlock)
            {
                weakSelf.addMusicBlock(musicName);
            }
        };
        
        [self.musicView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(weakSelf.mas_bottom).offset(-20);
            make.width.left.equalTo(weakSelf);
            make.height.mas_equalTo(150);
        }];
    }
    return _musicView;
}



-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor whiteColor];
        
        
        [self addSubview:self.soundContentView];

        self.progressView = [HRProgressBackView new];
        [self addSubview:self.progressView];
        
        
        
        
        
        UIButton *recordButton = [UIButton buttonWithType:0];
        [self addSubview:recordButton];
        [recordButton setImage:[UIImage imageNamed:@"录音按钮"] forState:0];
        [recordButton setImage:[UIImage imageNamed:@"停止录音"] forState:UIControlStateSelected];
        
        recordButton.layer.borderColor = [UIColor whiteColor].CGColor;
        recordButton.layer.borderWidth = 0.9;
        [recordButton addTarget:self action:@selector(recordButtonClick) forControlEvents:UIControlEventTouchUpInside];
        recordButton.layer.cornerRadius = 33;
        recordButton.clipsToBounds = YES;
        self.recordButton = recordButton;
        
        
        self.againRecordButton = [UIButton buttonWithType:0];
        [self.againRecordButton setTitle:@"重新录制" forState:0];
        self.againRecordButton.titleLabel.font = CustomUIFont(13);
        [self.againRecordButton setTitleColor:CustomColor(28, 190, 160, 1) forState:0];
        [self addSubview:self.againRecordButton];
        [self.againRecordButton addTarget:self action:@selector(againRecordButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:self.musicView];
        
        
        WeakSelf
        [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(weakSelf);
            make.size.mas_equalTo(CGSizeMake(120, 120));
            make.centerY.equalTo(weakSelf).offset(-50);
        }];
        
        
        [recordButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(weakSelf);
            make.top.equalTo(weakSelf.progressView.mas_bottom).offset(20);
            make.size.mas_equalTo(CGSizeMake(80, 80));
        }];
        
        [self.againRecordButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(recordButton.imageView.mas_right);
            make.top.equalTo(recordButton.imageView.mas_bottom).offset(-20);
            make.size.mas_equalTo(CGSizeMake(100, 20));
        }];


        
    }
    return self;
}

- (void)nextButtonClick:(UIButton *)button
{
    if (self.recordButton.selected){
        [MBProgressHUD showMessage:@"请先暂停录制在进行下一步操作" finishBlock:nil];
        return;
    }
    button.enabled = NO;
    
    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0/*延迟执行时间*/ * NSEC_PER_SEC));
    
    dispatch_after(delayTime, dispatch_get_main_queue(), ^{
        button.enabled = YES;
    });
    
}


- (void)againRecordButtonClick{
    
    self.recordButton.selected = YES;
    
    self.progressView.circleView.timeLabel.text = @"0S";
    if (self.againRecordButtonClickBlock){
        self.againRecordButtonClickBlock();
    }
    [self.progressView removeTimer];
    [self.progressView againRecord];
    [self.progressView addTimer];
   
    
}


-(void)recordAudioStop
{
//    [self againRecordButtonClick];
}





- (void)recordButtonClick
{
    [self.progressView removeTimer];

    if (self.recordButton.selected){
        self.recordButton.selected = NO;

        [self.progressView removeTimer];
    }else{
        self.recordButton.selected = YES;
        [self.progressView addTimer];

        
    }
    if (self.recordAudioBlock)
    {
        self.recordAudioBlock();
    }
    
}
- (void)playButtonClick:(UIButton *)button
{
    if (button.selected)
    {
        button.selected = NO;
        if (self.isPlayAudioBlock)
        {
            self.isPlayAudioBlock(NO);
        }
    }else{
        button.selected = YES;
        if (self.isPlayAudioBlock)
        {
            self.isPlayAudioBlock(YES);
        }
    }
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self endEditing:NO];
}




@end
